package com.exercises;


import java.util.*;

/**
 * 并归排序
 */
class Exercise1418 {


    public List<List<String>> displayTable(List<List<String>> orders) {
        Set<String> head = new TreeSet<>();
        Set<Integer> table = new TreeSet<>();
        Map<String, Integer> map = new HashMap<>();
        orders.forEach(l -> {
            String foodItem = l.get(2);
            String tableNum = l.get(1);
            head.add(foodItem);
            table.add(Integer.parseInt(tableNum));
            String key = tableNum + "-" + foodItem;
            map.put(key, map.getOrDefault(key, 0) + 1);
        });
        List<List<String>> result = new ArrayList<>();
        List<String> column1 = new ArrayList<>();
        column1.add("Table");
        column1.addAll(head);
        result.add(column1);
        for (Integer t : table) {
            List<String> column2 = new ArrayList<>();
            column2.add(t + "");
            for (String h : head) {
                String key = t + "-" + h;
                column2.add(map.getOrDefault(key, 0) + "");
            }
            result.add(column2);
        }
        return result;
    }
}

